<template>
  <div>
    <!-- 我的歌手 -->
    <h2 class="usersub" v-for="(item, index) in filterList" :key="index">
      <router-link :to="{path:item.to}" class="f-fs1 s-fc0 f-ff1">{{ item.title }}({{ item.num }})</router-link>
    </h2>
    <!-- 创建的歌单 -->
    <div
      class="n-minelst n-minelst-1 n-minelst-hide"
      style="margin-top: 8px"
      v-if=" playlistData.mycreated_list.length"
    >
      <h2 class="f-ff1">
        <a
          hidefocus="true"
          href="javascript:;"
          class="u-btn u-btn-crt f-fr j-flag"
          @click.stop="createList"
          ><i>新建</i></a
        >
        <span class="rtitle f-pr" @click.stop="flag = !flag">
          <i :class="['tri', { tri1: !flag }, { tri2: flag }]"></i>
          创建的歌单
          <span
            class="f-pa f-r-black-icon"
            style="
              display: inline-block;
              width: 8px;
              height: 8px;
              background-size: cover;
            "
          >
          </span>
          <span class="f-r-icon-dis-5"></span>
          (
          <span class="j-flag">{{ playlistData.mycreated_list.length }}</span>
          )
        </span>
      </h2>
      <ul class="j-flag f-cb" v-show="flag">
        <li
           :class="['j-iflag', {'z-selected' : index == BGflag}]"
          v-for="(item, index) in playlistData.mycreated_list"
          :key="index"
          @click.stop="toRouter({path:'/my/playlist' , query:{id:item.id , index : index}})"
        >
          <div class="item f-cb">
            <div class="left">
              <a class="avatar">
                <img :src="item.coverImgUrl" alt="" />
              </a>
            </div>
            <p class="name f-thide">
              <a href="javascript:;" class="s-fc0" title="我喜欢的音乐">{{
                item.name
              }}</a>
            </p>
            <p class="s-fc4 f-thide num">{{ item.trackCount }}首</p>
          </div>
          <span class="oper hshow">
            <a href="javascript:;" title="编辑" class="u-icn u-icn-10"></a>
            <a
              href="javascript:;"
              title="删除"
              class="u-icn u-icn-11"
              @click.stop="del(item.id, 'create')"
            ></a>
          </span>
        </li>
      </ul>
    </div>
    <!-- 收藏的歌单 -->
    <div
      class="n-minelst n-minelst-1 n-minelst-hide"
      style="margin-top: 8px"
      v-if=" playlistData.mysub_list.length"
    >
      <h2 class="f-ff1">
        <span class="rtitle f-pr" @click.stop="Subflag = !Subflag">
          <i :class="['tri', { tri1: !Subflag }, { tri2: Subflag }]"></i>
          收藏的歌单
          <span
            class="f-pa f-r-black-icon"
            style="
              display: inline-block;
              width: 8px;
              height: 8px;
              background-size: cover;
            "
          >
          </span>
          <span class="f-r-icon-dis-5"></span>
          (
          <span class="j-flag">{{ playlistData.mysub_list.length }}</span>
          )
        </span>
      </h2>
      <ul class="j-flag f-cb" v-show="Subflag">
        <li
          v-for="(item, index) in playlistData.mysub_list"
          :class="['j-iflag', {'z-selected' : index+10 == BGflag}]"
          :key="index"
          @click.stop="toRouter({path:'/my/playlist' , query:{id:item.id , index : index+10}})"
        >
          <div class="item f-cb">
            <div class="left">
              <a class="avatar">
                <img :src="item.coverImgUrl" alt="" />
              </a>
            </div>
            <p class="name f-thide">
              <a href="javascript:;" class="s-fc0" title="我喜欢的音乐">{{
                item.name
              }}</a>
            </p>
            <p class="s-fc4 f-thide num">
              {{ item.trackCount }}首 &nbsp;by {{ item.creator.nickname }}
            </p>
          </div>
          <span class="oper hshow">
            <a
              href="javascript:;"
              title="删除"
              class="u-icn u-icn-11"
              @click.stop="del(item.id, 'sub')"
            ></a>
          </span>
        </li>
      </ul>
    </div>
    <div style="height: 100px"></div>
  </div>
</template>

<script>
import { userSubcount } from "../../network/login";
import { userPlaylist } from "../../network/login"; //我创建的/我收藏的
import { ParseIntnuber } from "../../components/tool/tool";
import { playlist_delete } from "../../network/song"; //删除歌单
export default {
  props: ['userId'],
  data() {
    return {
      sidebarList: [], //侧边栏各个专区的内容数量
      playlistData:{mycreated_list:[] , mysub_list:[]}, //我创建的歌单 和 我收藏的歌单
      flag: true, //展开创建歌单按钮
      Subflag: true, //展开收藏歌单按钮
      BGflag:0
    };
  },
  methods: {
    del(id, type) {
      //删除歌单
      this.$remind.show("确定要删除此歌单 ？", (status) => {
        if (status == 200) {
          playlist_delete(id).then((res) => {
            if (type == "create") {
              this.playlistData.mycreated_list.forEach((item, index) => {
                if (item.id == id) {
                  this.playlistData.mycreated_list.splice(index, 1);
                }
              });
            } else if (type == "sub") {
              this.playlistData.mysub_list.forEach((item, index) => {
                if (item.id == id) {
                  this.playlistData.mysub_list.splice(index, 1);
                }
              });
            }
          });
        }
      });
    },
    resslidebar() {
      //获取 我的歌手 我的视频 我的电台
      userSubcount().then((res) => {
        this.sidebarList.push(
          { title: "我的电台", num: res.data.djRadioCount, to: "/my/Radio" }, //订阅的电台
          { title: "我的视频", num: res.data.mvCount, to: "/my/mv" }, //mv
          {title: "我创建的电台",num: res.data.createDjRadioCount, to: "/my/createRadio",}, //创建的电台
          { title: "我的歌手", num: res.data.artistCount, to: "/my/artist" } //我的歌手
        );
      });
    },
    userList(id){       //获取创建歌单和收藏歌单
      userPlaylist(id).then(res => {
         if(!this.$route.query.id){
             this.toRouter({path:'/my/playlist' , query:{id:res.data.playlist[0].id , index : 0}})
           }
        if (res.data.playlist.length) {    //如果创建和收藏的歌单存在
            res.data.playlist.forEach(item => {
              if(item.userId == id){
                this.playlistData.mycreated_list.push(item) //created
              }else{
                this.playlistData.mysub_list.push(item)   //sub
              }
           });
        }else{
           console.log("还没有创建歌单和收藏歌单")
        }
      })
    },
    toRouter(obj){   //路由的跳转
       this.$router.push(obj)
       this.BGflag = obj.query.index
    },
    createList() {
      //创建歌单
      this.$playlistCreated.isshow((data) => {
        if (data) {
          this.playlistData.mycreated_list.splice(1, 0, data);
          this.$parent.changeView({ tag: "mysong", id: data.id });
        }
      });
    },
  },
  computed: {
    filterList() {
      return this.sidebarList.filter((item) => {
        return item.num;
      });
    },
  },
  created() {
      this.resslidebar();
      this.userList(this.userId)
      this.BGflag = this.$route.query.index ? this.$route.query.index :0
  },
};
</script>

<style scoped>
h2 {
  font-size: 100%;
  padding: 0;
  margin: 0;
}
.n-musicsd .usersub {
  position: relative;
  height: 36px;
  line-height: 36px;
  color: #333;
  font-family: Arial, Helvetica, sans-serif;
}
a {
  cursor: pointer;
}
.n-musicsd .usersub a {
  display: block;
  padding: 0 5px 2px 35px;
}
.s-fc0 {
  color: #000;
}
.f-fs1 {
  font-size: 14px;
}
.f-ff1 {
  font-family: simsun, \5b8b\4f53;
}

.n-minelst h2 {
  padding: 0 10px 12px 4px;
  font-size: 14px;
  color: #000;
}
.f-ff1 {
  font-family: simsun, \5b8b\4f53;
}
.u-btn {
  display: inline-block;
}
.u-btn-crt {
  width: 52px;
  height: 22px;
  background: url("../../assets/imgs/button.png") no-repeat;
  background-position: 0 -96px;
}
.f-fr {
  float: right;
}
.u-btn-crt i {
  display: inline-block;
  line-height: 22px;
  overflow: hidden;
  padding-left: 20px;
  color: #515151;
  font-size: 12px;
  font-weight: normal;
  font-style: normal;
  text-align: left;
}
.f-pr {
  position: relative;
  zoom: 1;
}
.n-minelst .rtitle {
  cursor: pointer;
}
.n-minelst-hide .tri1 {
  border: 8px solid #ccc;
  border-color: transparent transparent transparent #ccc;
  border-style: dashed dashed dashed solid;
  margin-right: 0;
}
.tri2 {
  border-color: #ccc transparent transparent;
  border-style: solid dashed dashed;
  border-width: 10px 7px 0;
}
.n-minelst .tri {
  display: inline-block;
  vertical-align: middle;
  margin: 0 2px 0 5px;
  cursor: pointer;
  font-size: 0;
  height: 0;
  line-height: 0;
}
.f-r-black-icon {
  background: url("../../assets/imgs/black-r-icon@3x.png");
}
.f-pa {
  position: absolute;
  top: -3px;
  right: 34px;
}

.n-minelst ul {
  vertical-align: middle;
}

.n-minelst li {
  position: relative;
  zoom: 1;
  height: 42px;
  padding: 6px 0 6px 20px;
}
.n-minelst li:hover {
  background: #e6e6e6;
  cursor: pointer;
}
.n-minelst li.z-selected  {
    background: #e6e6e6;
}
.n-minelst li:hover :nth-child(2) {
  display: block;
}
.n-minelst .item {
  padding-left: 50px;
}
.n-minelst .left {
  display: inline;
  float: left;
  margin-left: -50px;
  overflow: hidden;
  width: 40px;
}
.n-minelst .avatar {
  display: block;
  position: relative;
  width: 40px;
  height: 40px;
}
.n-minelst .avatar img {
  width: 40px;
  height: 40px;
}
.n-minelst .name {
  width: 150px;
  overflow: hidden;
  margin-top: 2px;
  margin-bottom: 8px;
}
p {
  margin: 0;
  padding: 0;
}
.f-thide {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-wrap: normal;
}
.n-minelst .name a {
  white-space: nowrap;
}
.s-fc0 {
  color: #000;
  font-size: 12px;
  font-family: Arial, Helvetica, sans-serif;
}
.n-minelst .num {
  width: 130px;
  font-size: 12px;
  font-family: Arial, Helvetica, sans-serif;
}
.s-fc4 {
  color: #999;
}
.f-thide {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-wrap: normal;
}
.u-scroll::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
.u-scroll::-webkit-scrollbar-thumb {
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: 10px;
  box-shadow: inset 1px 1px 0 rgb(0 0 0 / 10%);
}
.u-scroll::-webkit-scrollbar-track {
  border-radius: 10px;
  box-shadow: inset 0 0 6px rgb(0 0 0 / 0%);
}
.n-minelst .oper {
  position: absolute;
  bottom: 7px;
  right: 0;
}
.n-minelst .hshow {
  display: none;
}
.n-minelst .oper .u-icn {
  float: left;
  margin-right: 10px;
}
.n-minelst ul * {
  cursor: pointer;
  vertical-align: middle;
}
.u-icn {
  display: inline-block;
  overflow: hidden;
  background: url("../../assets/imgs/icon.png") no-repeat 0 9999px;
}
.u-icn-10 {
  width: 13px;
  height: 13px;
  background-position: 0 -270px;
}
.u-icn-10:hover {
  background-position: -20px -270px;
}
.u-icn-11 {
  width: 13px;
  height: 14px;
  background-position: 0 -284px;
}
.u-icn-11:hover {
  background-position: -20px -284px;
}
</style>